package org.summer.lawyer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.bo.userwallet.UserWalletBo;
import org.summer.lawyer.domain.vo.userwallet.UserWalletVo;
import org.summer.lawyer.service.IUserWalletService;

import java.util.List;

/**
 * 用户钱包
 *
 * @author summer
 * @date 2024-06-27
 */
@Tag(name = "后台相关接口", description = "用户钱包")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/userWallet")
public class UserWalletController extends BaseController {

    private final IUserWalletService userWalletService;

    /**
     * 查询用户钱包列表
     */
    @Operation(summary = "查询用户钱包列表接口", description = "查询用户钱包列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:wallet:list")
    @GetMapping("/list")
    public TableDataInfo<UserWalletVo> list(UserWalletBo bo, PageQuery pageQuery) {
        return userWalletService.selectPageList(bo, pageQuery);
    }

    /**
     * 导出用户钱包列表
     */
    @Operation(summary = "导出用户钱包列表接口", description = "导出用户钱包列表")
    @SaCheckPermission("lawyer:wallet:export")
    @Log(title = "用户钱包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserWalletBo bo, HttpServletResponse response) {
        List<UserWalletVo> list = userWalletService.selectUserWalletVoList(bo);
        ExcelUtil.exportExcel(list, "用户钱包", UserWalletVo.class, response);
    }

    /**
     * 获取用户钱包详细信息
     *
     * @param walletId 主键
     */
    @Operation(summary = "获取用户钱包详细信息接口", description = "查询用户钱包列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:wallet:query")
    @GetMapping("/{walletId}")
    public R<UserWalletVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long walletId) {
        return R.ok(userWalletService.selectUserWalletVoById(walletId));
    }

    /**
     * 新增用户钱包
     */
    @Operation(summary = "新增用户钱包接口", description = "新增用户钱包", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:wallet:add")
    @Log(title = "用户钱包", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveUserWallet")
    public R<Void> saveUserWallet(@Validated(AddGroup.class) @RequestBody UserWalletBo bo) {
        return toAjax(userWalletService.insertByBo(bo));
    }

    /**
     * 修改用户钱包
     */
    @Operation(summary = "修改用户钱包接口", description = "修改用户钱包", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:wallet:edit")
    @Log(title = "用户钱包", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateUserWallet")
    public R<Void> updateUserWallet(@Validated(EditGroup.class) @RequestBody UserWalletBo bo) {
        return toAjax(userWalletService.updateByBo(bo));
    }

    /**
     * 删除用户钱包
     *
     * @param walletIds 主键串
     */
    @Operation(summary = "删除用户钱包接口", description = "删除用户钱包", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:wallet:remove")
    @Log(title = "用户钱包", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeUserWallet/{walletIds}")
    public R<Void> removeUserWallet(@NotEmpty(message = "主键不能为空")
                                    @PathVariable Long[] walletIds) {
        return toAjax(userWalletService.deleteWithValidByIds(List.of(walletIds), true));
    }
}
